package com.hqyj.common.service.impl;

import com.hqyj.common.dao.AnswerMapper;
import com.hqyj.common.dao.ShortTopicMapper;
import com.hqyj.common.pojo.Answer;
import com.hqyj.common.pojo.BaseTopic;
import com.hqyj.common.dao.BaseTopicMapper;
import com.hqyj.common.pojo.SysUser;
import com.hqyj.common.pojo.ShortTopic;
import com.hqyj.common.service.IBaseTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * <p>
 * 基础题库 服务实现类
 * </p>
 *
 * @author j0701-9
 * @since 2021-10-20
 */
@Service

public class BaseTopicServiceImpl  implements IBaseTopicService {
    @Autowired
    BaseTopicMapper baseTopicMapper;
    @Autowired
    ShortTopicMapper shortTopicMapper;
    @Autowired
    AnswerMapper answerMapper;
    /**
     * 随机生成试卷，生成试卷同时生成答题卡
     * @param testPaper  生成试卷名称，用于生成答题卡
     * @param oneQuantity  生成试卷单选题数量
     * @param ManyQuantity  生成试卷多选题数量
     * @param judgeQuantity   生成试卷判断题数量
     * @param briefQuantity   生成试卷简答题数量
     * @return
     */
    @Override
    public HashMap<String, Object> generatePaperMap(String testPaper, int oneQuantity,
                                                    int ManyQuantity, int judgeQuantity,
                                                    int briefQuantity, HttpSession session) {
        HashMap<String,Object> map = new HashMap<>();
        // 试卷生成
        ArrayList<BaseTopic> singleChoice = baseTopicMapper.randomSelectSingleChoice(oneQuantity);
        ArrayList<BaseTopic> multipleChoice = baseTopicMapper.randomSelectMultipleChoice(ManyQuantity);
        ArrayList<BaseTopic> judge = baseTopicMapper.randomSelectJudge(judgeQuantity);
        ArrayList<ShortTopic> brief = shortTopicMapper.randomSelectBrief(briefQuantity);

        map.put("singleChoice",singleChoice);
        map.put("multipleChoice",multipleChoice);
        map.put("judge",judge);
        map.put("brief",brief);
        map.put("test_paper",testPaper);
        // 答题卡生成
        Answer answer = new Answer();
        SysUser user = (SysUser) session.getAttribute("user");
        answer.setUserId(user.getUserId());
        answer.setTestPaper(testPaper);
        answerMapper.insert(answer);
        return map;
    }
}
